// Copyright (c) Open Enclave SDK contributors.
// Licensed under the MIT License.

#include <openenclave/host.h>
#include <openenclave/internal/hexdump.h>
#include <openenclave/internal/tests.h>
#include "hexdump_u.h"

#define DATA_SIZE 1024

/* Randomly generated data */
static const unsigned char _data[DATA_SIZE] = {
    0x47, 0x40, 0x8f, 0xe5, 0xef, 0x30, 0x2c, 0x72, 0x54, 0x4a, 0x43, 0x9b,
    0xec, 0xb8, 0x1c, 0x78, 0xd8, 0xe1, 0x54, 0xcd, 0x46, 0x3d, 0x2c, 0x83,
    0x62, 0x95, 0x0c, 0x85, 0x16, 0x3b, 0x22, 0xe6, 0x78, 0xa4, 0x5a, 0xac,
    0x3a, 0xe9, 0x03, 0x72, 0x85, 0xac, 0xe6, 0x0f, 0x93, 0x60, 0x78, 0xe0,
    0x82, 0x6b, 0xc1, 0x96, 0x51, 0x74, 0x34, 0xe8, 0x1b, 0xe7, 0xd2, 0x37,
    0xbb, 0x3f, 0x05, 0x23, 0x9a, 0x47, 0xa5, 0x32, 0x0f, 0x0a, 0x2e, 0xaa,
    0x9f, 0xb6, 0xea, 0x4c, 0x3b, 0xfa, 0xf9, 0x87, 0xf0, 0xbe, 0xd3, 0xdc,
    0xf5, 0x04, 0xc1, 0x4a, 0x4b, 0x03, 0x8e, 0x4e, 0x2b, 0xfb, 0x64, 0x66,
    0x3e, 0xed, 0x2b, 0x3c, 0x86, 0x09, 0x46, 0xc6, 0xb2, 0x40, 0x71, 0xdd,
    0xa1, 0x5a, 0x7d, 0xe0, 0x52, 0xa5, 0x91, 0xa5, 0xab, 0xe7, 0x87, 0x00,
    0x51, 0x3e, 0x5a, 0x79, 0x94, 0x15, 0xe7, 0x27, 0x7f, 0x73, 0x0a, 0x01,
    0x7f, 0x8b, 0x32, 0xa8, 0xb2, 0x32, 0x88, 0xd9, 0x0f, 0x67, 0x51, 0x84,
    0xd3, 0xd6, 0x91, 0x6c, 0x18, 0xe7, 0xa8, 0x1a, 0x96, 0x65, 0xe8, 0xbf,
    0x06, 0xa1, 0xef, 0xb7, 0x9f, 0xca, 0xe2, 0xf1, 0x50, 0xc6, 0xe2, 0x46,
    0x16, 0xc0, 0x69, 0x5b, 0xa6, 0x36, 0x1c, 0x0f, 0x0e, 0x4f, 0xc8, 0x13,
    0x49, 0x12, 0x47, 0x28, 0xe1, 0xcc, 0x3b, 0x99, 0x66, 0x37, 0xfc, 0xf8,
    0x3f, 0x4f, 0x48, 0xe9, 0xc4, 0x8e, 0x55, 0x32, 0xfa, 0xc3, 0x24, 0xe6,
    0xb7, 0x79, 0x94, 0x50, 0x17, 0x7b, 0x52, 0x6a, 0xb2, 0xad, 0x7d, 0x2e,
    0xa9, 0x9b, 0xa9, 0x56, 0xff, 0x0e, 0xa5, 0x1a, 0x49, 0x7a, 0x82, 0x9a,
    0xa4, 0x99, 0x84, 0x11, 0xeb, 0xf3, 0x57, 0x30, 0x6a, 0xfc, 0xc0, 0xd8,
    0x9f, 0x01, 0xe4, 0x35, 0xa7, 0xc9, 0xe1, 0xce, 0x5e, 0xce, 0xc4, 0xf4,
    0xf8, 0x92, 0x39, 0x8f, 0x73, 0x4c, 0x76, 0x41, 0x56, 0x5e, 0xa1, 0x33,
    0xdd, 0xeb, 0xca, 0xa7, 0x1e, 0x2c, 0xa9, 0x44, 0xc2, 0x49, 0x9d, 0xed,
    0x1c, 0xef, 0x9c, 0xa8, 0xc3, 0xe6, 0x39, 0x2c, 0xbc, 0x4a, 0xe5, 0xa0,
    0x2d, 0x32, 0xcf, 0x94, 0xbc, 0x2b, 0x4e, 0xc1, 0x92, 0x6d, 0x5a, 0xb7,
    0x79, 0x1c, 0x1d, 0x73, 0x6c, 0xa6, 0xfc, 0x69, 0xf8, 0xbc, 0x98, 0x03,
    0x23, 0x4e, 0x99, 0x1a, 0x9b, 0xa3, 0x21, 0x72, 0xfb, 0x05, 0x48, 0xdb,
    0x14, 0x73, 0xa2, 0x46, 0xe2, 0x36, 0x90, 0xcd, 0x38, 0xaa, 0x5d, 0x6b,
    0x27, 0xcc, 0x83, 0x90, 0xfe, 0x0d, 0xb9, 0x64, 0x92, 0x1a, 0xb1, 0x59,
    0xf6, 0x88, 0xd6, 0xfc, 0xeb, 0x97, 0x90, 0x77, 0xfc, 0xb9, 0xd4, 0xbd,
    0x3b, 0x92, 0xe7, 0x6c, 0x19, 0xd3, 0x95, 0xc1, 0x96, 0x89, 0xbf, 0x6b,
    0x5f, 0xd9, 0x80, 0xff, 0x07, 0xdd, 0x9c, 0x3b, 0xbc, 0x76, 0x6f, 0x32,
    0x13, 0x35, 0x0b, 0xa1, 0x17, 0x2a, 0x1e, 0x17, 0x77, 0x6e, 0x63, 0xc8,
    0x96, 0x0c, 0x35, 0x6f, 0xe3, 0xc5, 0x22, 0xa5, 0x75, 0xfe, 0xa3, 0x8c,
    0xa8, 0x41, 0x35, 0x15, 0x13, 0x3b, 0x5b, 0x42, 0x25, 0x3d, 0xb6, 0xec,
    0x7a, 0x86, 0x41, 0xcb, 0xe9, 0x0c, 0xa7, 0x7e, 0xd8, 0x97, 0x26, 0xb5,
    0x52, 0x91, 0x6f, 0xce, 0x71, 0x3b, 0xbe, 0x0f, 0x51, 0xad, 0xa0, 0x12,
    0xb6, 0xdd, 0x22, 0x9d, 0x19, 0xd1, 0x32, 0xc4, 0x8d, 0x1e, 0x67, 0x37,
    0x3b, 0x1a, 0x7c, 0x05, 0xdb, 0xd3, 0xd8, 0xe0, 0xac, 0x18, 0x74, 0xb3,
    0x95, 0xe6, 0xc3, 0x58, 0xf0, 0x82, 0x07, 0xfd, 0x7e, 0x45, 0x91, 0x8e,
    0x9b, 0x83, 0x45, 0xa7, 0x3b, 0xe0, 0xde, 0xb2, 0x0d, 0x00, 0x5f, 0x79,
    0x56, 0x1f, 0x77, 0x5a, 0xf2, 0x0a, 0x87, 0x00, 0x68, 0x0f, 0x50, 0x0b,
    0x59, 0x2a, 0x16, 0x74, 0x23, 0xc5, 0x80, 0xed, 0xe8, 0x36, 0xd2, 0x13,
    0xb5, 0x67, 0xa1, 0xad, 0x77, 0xaf, 0xc4, 0x76, 0x34, 0xb3, 0x09, 0x93,
    0x43, 0x3f, 0x76, 0x7a, 0xe7, 0xc6, 0xbd, 0x5b, 0xac, 0x99, 0xf6, 0xe1,
    0x03, 0x4a, 0x0a, 0xe3, 0xcc, 0x89, 0x74, 0x39, 0x2f, 0x09, 0xbb, 0xe6,
    0x7c, 0x80, 0x38, 0x66, 0x06, 0xd6, 0xa9, 0x4f, 0xc2, 0x11, 0x0e, 0x9e,
    0xa1, 0x5d, 0x9a, 0x8d, 0xee, 0x08, 0x31, 0x9d, 0x8c, 0x25, 0x28, 0xdc,
    0x9f, 0x95, 0x60, 0x2b, 0x9c, 0x02, 0xca, 0x22, 0xd0, 0xe9, 0x39, 0x4f,
    0x8f, 0xe2, 0xd7, 0x92, 0x65, 0x65, 0x57, 0x60, 0xc7, 0x9d, 0xa7, 0x8b,
    0x01, 0x5e, 0x6e, 0x40, 0x2f, 0x77, 0xf3, 0x48, 0x27, 0x35, 0xb4, 0xe3,
    0x56, 0x7b, 0x69, 0x53, 0xa3, 0xfc, 0xdb, 0xe8, 0x94, 0x53, 0xc4, 0x70,
    0xda, 0x78, 0x24, 0xde, 0x3f, 0xdc, 0x21, 0x12, 0xea, 0x28, 0x13, 0x28,
    0x21, 0xd1, 0x6d, 0x18, 0x02, 0x47, 0x31, 0x6e, 0xf8, 0xf7, 0x45, 0x83,
    0x9f, 0x8d, 0x05, 0x1e, 0xf9, 0x4e, 0x52, 0x9e, 0x34, 0x5d, 0x3b, 0xa8,
    0x97, 0xcd, 0x30, 0xb9, 0x1e, 0x47, 0xdc, 0x2f, 0x5a, 0x22, 0x70, 0xb3,
    0x36, 0x16, 0xa6, 0x16, 0x52, 0xde, 0x96, 0xa1, 0xf5, 0x41, 0x04, 0x71,
    0x2a, 0xc4, 0x45, 0xf8, 0x8b, 0x42, 0x65, 0x5d, 0x85, 0x96, 0xb1, 0x14,
    0xff, 0x71, 0xe3, 0xe3, 0xb4, 0x4a, 0x46, 0x41, 0x24, 0x53, 0xfe, 0xa7,
    0xa6, 0xc6, 0x13, 0x22, 0x79, 0x31, 0xea, 0xfb, 0x8e, 0x8e, 0x41, 0x1f,
    0xbb, 0x26, 0x81, 0x44, 0x9a, 0xc8, 0x7e, 0x4f, 0xf5, 0x73, 0xce, 0x6c,
    0xea, 0x0a, 0x10, 0x36, 0xb4, 0x7c, 0x76, 0x2d, 0xbd, 0xf8, 0xd3, 0xf5,
    0x0c, 0x85, 0xce, 0xce, 0xf3, 0xe5, 0x9e, 0x37, 0xea, 0xee, 0x4d, 0x64,
    0xf4, 0xf3, 0x30, 0xa5, 0x85, 0x84, 0xd5, 0xe3, 0x30, 0x4b, 0xe9, 0xab,
    0x0e, 0x5f, 0x12, 0x59, 0x4c, 0xb6, 0x61, 0xfe, 0xce, 0x59, 0x3e, 0x9e,
    0xd5, 0x09, 0x1d, 0xb9, 0x3a, 0x6b, 0x7b, 0xc6, 0x2c, 0x4f, 0x6e, 0xdc,
    0x67, 0xaa, 0x37, 0xe0, 0xd6, 0x88, 0xc7, 0x31, 0xba, 0x64, 0x7b, 0xce,
    0xca, 0xf8, 0x69, 0x34, 0xd1, 0xcb, 0x28, 0xc3, 0x35, 0x29, 0x55, 0x54,
    0xe4, 0xa8, 0xf2, 0x49, 0x31, 0xc6, 0x2c, 0xa8, 0x39, 0x85, 0x76, 0x06,
    0xd6, 0x78, 0x96, 0x58, 0xc0, 0x09, 0x1a, 0xf7, 0xd1, 0xcc, 0xf0, 0x41,
    0xef, 0x4d, 0x8a, 0xf7, 0x62, 0x9e, 0x51, 0x79, 0xfb, 0xf9, 0x2b, 0x54,
    0xc6, 0xd4, 0x67, 0x83, 0x0b, 0x87, 0x38, 0xa8, 0x6c, 0xfd, 0xce, 0x55,
    0xa2, 0xa4, 0xa5, 0x2b, 0x32, 0x66, 0xb3, 0xed, 0xac, 0x7e, 0x9a, 0x43,
    0xd7, 0xfc, 0x28, 0xd5, 0x17, 0x0c, 0xab, 0xed, 0x34, 0x4d, 0x56, 0x4a,
    0x9b, 0xf8, 0x92, 0x1b, 0x5b, 0x83, 0x91, 0x69, 0x17, 0xcb, 0x46, 0xdb,
    0x4e, 0xa2, 0x9a, 0x16, 0x4b, 0xbb, 0xf7, 0xeb, 0x86, 0x16, 0x6b, 0x3f,
    0xac, 0x1e, 0xfd, 0x5e, 0x0e, 0xf5, 0x87, 0xa8, 0xc4, 0xf7, 0x98, 0x37,
    0x18, 0x67, 0x6d, 0xab, 0x26, 0x42, 0xa6, 0xca, 0x9a, 0xa6, 0x9d, 0x24,
    0xd1, 0x7f, 0x27, 0x46, 0x8b, 0x13, 0xc5, 0x4f, 0xb2, 0x8e, 0x37, 0xb7,
    0x8f, 0x50, 0xd0, 0xed, 0x8f, 0xf9, 0x1a, 0x75, 0x17, 0x6a, 0x23, 0x00,
    0x91, 0x53, 0x62, 0x75, 0xea, 0x35, 0x14, 0x0b, 0x01, 0x2f, 0x8b, 0x5d,
    0xbf, 0xe6, 0x4a, 0x56, 0xd7, 0x3d, 0xb0, 0x45, 0xa9, 0x74, 0x86, 0xc9,
    0xa4, 0x30, 0xa6, 0x83, 0xdb, 0xee, 0xc8, 0x0c, 0x95, 0x4f, 0xda, 0x5a,
    0xf1, 0x43, 0x02, 0xc4, 0x12, 0xdc, 0xb7, 0x79, 0x02, 0x4b, 0x5c, 0xb3,
    0xba, 0x8c, 0xe9, 0x15, 0xae, 0xab, 0x79, 0xd6, 0x9a, 0x2f, 0xde, 0xe7,
    0xeb, 0xfe, 0x8a, 0xe9,
};

/* Hex string of _data[] */
static const char _hexstr[2 * DATA_SIZE + 1] =
    "47408fe5ef302c72544a439becb81c78d8e154cd463d2c8362950c85163b22e6"
    "78a45aac3ae9037285ace60f936078e0826bc196517434e81be7d237bb3f0523"
    "9a47a5320f0a2eaa9fb6ea4c3bfaf987f0bed3dcf504c14a4b038e4e2bfb6466"
    "3eed2b3c860946c6b24071dda15a7de052a591a5abe78700513e5a799415e727"
    "7f730a017f8b32a8b23288d90f675184d3d6916c18e7a81a9665e8bf06a1efb7"
    "9fcae2f150c6e24616c0695ba6361c0f0e4fc81349124728e1cc3b996637fcf8"
    "3f4f48e9c48e5532fac324e6b7799450177b526ab2ad7d2ea99ba956ff0ea51a"
    "497a829aa4998411ebf357306afcc0d89f01e435a7c9e1ce5ecec4f4f892398f"
    "734c7641565ea133ddebcaa71e2ca944c2499ded1cef9ca8c3e6392cbc4ae5a0"
    "2d32cf94bc2b4ec1926d5ab7791c1d736ca6fc69f8bc9803234e991a9ba32172"
    "fb0548db1473a246e23690cd38aa5d6b27cc8390fe0db964921ab159f688d6fc"
    "eb979077fcb9d4bd3b92e76c19d395c19689bf6b5fd980ff07dd9c3bbc766f32"
    "13350ba1172a1e17776e63c8960c356fe3c522a575fea38ca8413515133b5b42"
    "253db6ec7a8641cbe90ca77ed89726b552916fce713bbe0f51ada012b6dd229d"
    "19d132c48d1e67373b1a7c05dbd3d8e0ac1874b395e6c358f08207fd7e45918e"
    "9b8345a73be0deb20d005f79561f775af20a8700680f500b592a167423c580ed"
    "e836d213b567a1ad77afc47634b30993433f767ae7c6bd5bac99f6e1034a0ae3"
    "cc8974392f09bbe67c80386606d6a94fc2110e9ea15d9a8dee08319d8c2528dc"
    "9f95602b9c02ca22d0e9394f8fe2d79265655760c79da78b015e6e402f77f348"
    "2735b4e3567b6953a3fcdbe89453c470da7824de3fdc2112ea28132821d16d18"
    "0247316ef8f745839f8d051ef94e529e345d3ba897cd30b91e47dc2f5a2270b3"
    "3616a61652de96a1f54104712ac445f88b42655d8596b114ff71e3e3b44a4641"
    "2453fea7a6c613227931eafb8e8e411fbb2681449ac87e4ff573ce6cea0a1036"
    "b47c762dbdf8d3f50c85cecef3e59e37eaee4d64f4f330a58584d5e3304be9ab"
    "0e5f12594cb661fece593e9ed5091db93a6b7bc62c4f6edc67aa37e0d688c731"
    "ba647bcecaf86934d1cb28c335295554e4a8f24931c62ca839857606d6789658"
    "c0091af7d1ccf041ef4d8af7629e5179fbf92b54c6d467830b8738a86cfdce55"
    "a2a4a52b3266b3edac7e9a43d7fc28d5170cabed344d564a9bf8921b5b839169"
    "17cb46db4ea29a164bbbf7eb86166b3fac1efd5e0ef587a8c4f7983718676dab"
    "2642a6ca9aa69d24d17f27468b13c54fb28e37b78f50d0ed8ff91a75176a2300"
    "91536275ea35140b012f8b5dbfe64a56d73db045a97486c9a430a683dbeec80c"
    "954fda5af14302c412dcb779024b5cb3ba8ce915aeab79d69a2fdee7ebfe8ae9";

int main(int argc, const char* argv[])
{
    oe_result_t result;
    oe_enclave_t* enclave = NULL;

    /* Check command-line argument count */
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s ENCLAVE_PATH\n", argv[0]);
        return 1;
    }

    /* Create the enclave */
    const uint32_t flags = oe_get_create_flags();
    OE_TEST(
        (result = oe_create_hexdump_enclave(
             argv[1], OE_ENCLAVE_TYPE_AUTO, flags, NULL, 0, &enclave)) ==
        OE_OK);

    /* Test enclave version of oe_hex_string() */
    {
        char hexstr[sizeof(_hexstr)];
        int return_value;

        OE_TEST(
            (result = test(
                 enclave,
                 &return_value,
                 _data,
                 sizeof(_data),
                 hexstr,
                 sizeof(hexstr))) == OE_OK);
        OE_TEST(return_value == 0);
        OE_TEST(strcmp(hexstr, _hexstr) == 0);
    }

    /* Test host version of oe_hex_string() */
    {
        char hexstr[sizeof(_hexstr)];

        OE_TEST(oe_hex_string(hexstr, sizeof(hexstr), _data, sizeof(_data)));
        OE_TEST(strcmp(hexstr, _hexstr) == 0);

        oe_hex_dump(_data, DATA_SIZE);
    }

    /* Terminate the test enclave */
    oe_terminate_enclave(enclave);

    printf("=== passed all tests (hexdump)\n");

    return 0;
}
